apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper"], function(V, $api, $, app, service, Swiper) {
        var vm = {
            el: '.vue-content',
            data: {
                swiperImageArr: [], //--用于轮播展示
                imgArr: [], //--用于移动图片js
                saveImgArr: [], //--用于保存接口
                num: '',
                newAddArr: [], //--新添加的图片数组，主要用于第二次进来时保存添加图片的数组
                oldIndex: '',
                newIndex: '',
                mySwiper: ''
            },
            mounted: function() {
                var that = this;
                this.startSwiper();

                //--移动图片
                var el = document.getElementById('listWithHandle');
                var sortable = new Sortable(el, {
                    // group: {
                    //   name: "shared",
                    //   pull: "clone",
                    // },
                    onAdd: function(evt) {
                        alert('1');
                    },
                    onEnd: function( /**Event*/ evt) {
                        // var itemEl = evt.item;  // dragged HTMLElement
                        // evt.to;    // target list
                        // evt.from;  // previous list
                        // evt.oldIndex;  // element's old index within old parent
                        // evt.newIndex;  // element's new index within new parent

                        console.log(evt.oldIndex + ',' + evt.newIndex);
                        that.oldIndex = evt.oldIndex;
                        that.newIndex = evt.newIndex;

                        var imgOld = that.swiperImageArr[evt.oldIndex];
                        var imgNew = that.swiperImageArr[evt.newIndex];
                        that.swiperImageArr.splice(evt.oldIndex, 1);
                        that.swiperImageArr.splice(evt.newIndex, 0, imgOld)

                        console.log(that.imgArr);

                    },
                });

            },
            methods: {
                //--返回
                close: function() {
                    api.closeWin();
                },
                //--初始化轮播图
                startSwiper: function() {
                    var that = this;
                    var mySwiper = new Swiper('.swiper-container', {
                        pagination: {
                            el: '.swiper-pagination',
                            type: 'fraction'
                        },
                        observer: true,
                        observeParents: true,
                    })
                    this.mySwiper = mySwiper;
                },
                //--删除图片（swiper）
                swiperSliderDel: function(inds) {
                    this.imgArr = this.swiperImageArr.slice();

                    this.newAddArr.splice(inds, 1);
                    this.swiperImageArr.splice(inds, 1);
                    this.imgArr.splice(inds, 1);
                    this.saveImgArr.splice(inds, 1);
                    this.num--;
                },
                //--批量上传图片
                getPictureS: function() {
                    var that = this;

                    if (that.swiperImageArr.length > 0) {
                        that.swiperImageArr = that.imgArr.slice(0);
                        that.imgArr = that.swiperImageArr.slice(0);
                        console.log(JSON.stringify(this.imgArr));
                    }


                    app.getPictureS2(8, this.num, service['fileupload'], function(res) {
                        if (res.length > 0) {
                            api.toast({
                                msg: '上传成功',
                                duration: 2000,
                                location: 'middle'
                            });
                            console.log('本地图片是：' + JSON.stringify(res) + ', length: ' + res.length);
                            that.num = that.num + res.length;

                            that.newAddArr.push(res); //--把本地图片存入数组
                            that.newAddArr = that.newAddArr.join(",").split(",");

                            that.swiperImageArr.push(res); //--把本地图片存入数组
                            console.log('获取的newAddArr: ' + that.newAddArr);
                            console.log("第一步：" + that.swiperImageArr + " typeof: " + that.swiperImageArr.constructor + "1111\n");
                            var str4 = that.swiperImageArr.join(",");
                            console.log("第一步：" + str4 + " typeof: " + str4.constructor + "1111\n");
                            that.swiperImageArr = str4.split(",");
                            console.log("第三步：" + that.swiperImageArr + " typeof: " + that.swiperImageArr.constructor + "1111\n");

                            that.imgArr = that.swiperImageArr.slice(0); //--把本地图片存入数组
                            console.log('imgArr: ' + JSON.stringify(that.imgArr));

                            // console.log(that.swiperImageArr[0]+" &Length:"+that.swiperImageArr.length+" &typeof: "+typeof that.swiperImageArr +"\n")
                            // console.log(JSON.stringify(that.swiperImageArr) +" : "+that.swiperImageArr.constructor);
                            that.$nextTick(function() {
                                that.mySwiper.destroy(false);
                                $(".swiper-pagination").css("display", "block")
                                that.startSwiper() // swiper重新初始化

                                console.log("初始化！！！！！！！！！！！！！！！！！！！！！！！！！！");
                            })
                        }
                    })
                },
                //--保存
                uploadImgSave: function() {
                    var that = this;
                    var count = 0;
                    var emptyArr = [];

                    if (this.newAddArr.length > 0) {
                        // for(var i=0;i<this.newAddArr.length;i++){
                        app.upload2(this.newAddArr, this.newAddArr.length, 0, emptyArr, service['fileupload'], function(res) {
                            console.log('res: ' + JSON.stringify(res)); //--获取了上传服务器的图片路径
                            // count++;
                            // that.saveImgArr.push(res);	//把上传了服务器的图片存入数组
                            for (var i = 0; i < res.length; i++) {
                                that.saveImgArr.push(res[i]);
                            }

                            // if (count == that.newAddArr.length) {
                            app.unmask({})


                            api.sendEvent({
                                name: 'shopAddImgManage',
                                extra: {
                                    'imgList': that.saveImgArr,
                                    'num': that.saveImgArr.length
                                }
                            });
                            $api.setTimeout(function() {
                                api.closeWin();
                            }, 1000)

                            // }
                        });
                        // }
                    } else {
                        api.sendEvent({
                            name: 'shopAddImgManage',
                            extra: {
                                'imgList': that.swiperImageArr,
                                'num': that.swiperImageArr.length
                            }
                        });

                        api.closeWin();
                    }
                }

            },
            created: function() {
                var that = this;
                this.swiperImageArr = api.pageParam.imgArr; //--用于轮播展示

                this.imgArr = this.swiperImageArr.slice(); //--用于移动图片js
                this.saveImgArr = this.swiperImageArr.slice(); //--用于保存接口

                this.num = api.pageParam.imgArr.length
                console.log(this.swiperImageArr + ' : ' + this.num)


            }
        };

        var even = function() {

        };
        var newVue = new Vue(vm);
        even();

    })
}
